num ho2d	= 1;
num hMapd	= 1;
num hECTd	= 1;
num hIATd	= 1;
num hRPMd	= 1;
num hTPSd	= 1;
num hVSSd	= 1;
num hnTPS	= 1;
num hnTIP	= 1;
num hnRPM	= 1;

num hPortd	= 1;
num hWB		= 1;

num ho2de	= 1;
num hMapde	= 1;
num hECTde	= 1;
num hIATde	= 1;
num hRPMde	= 1;
num hTPSde	= 1;
num hVSSde	= 1;

num hScroll	= 0;
num nCom	= 0;
num nTpsTick	= 0;
num nLastTps	= 0;

num nGraphNo	= 0;


void ComSel()
{
	nCom = getitem(hPortd);
	SelectCom(nCom);
}

void LoadGraphBtn()
{
	OpenFile(0, "*.udg");
	OpenGraph();
	Refresh();
}

void SaveGraphBtn()
{
	SaveFile(0, "*.udg");
	SaveGraph();
}

void open()
{
	num x = 10;
	num y = 10;

	hScroll = CreateHScroll(150, 130, 700, 20, 8, "GraphScroll", "Bleh");
	CreateBtn(150, 110, 70, 20, 8, "LoadGraphBtn", "Load Graph");
	CreateBtn(220, 110, 70, 20, 8, "SaveGraphBtn", "Save Graph");
	SetItemEx(hScroll, 0, 0, 50);
	SetItemEx(hScroll, 1, 0);

	CreateBorder(x, y, 600, 85, 8, 0, "Sensors");
	CreateStatic(x+5, y-5, 100, 20, 8, 0, "Sensors");
	
	x = x + 20;
	y = y + 20;

	ho2d = CreateCheck(x, y, 130, 20, 8, 0, "O2");
	ho2de = CreateEdit(x+130, y, 50, 20, 8, 0, "0");
	setitem(ho2d, 0);

	y = y + 20;
	hMapd = CreateCheck(x, y, 130, 20, 8, 0, "MAP");
	hMapde = CreateEdit(x+130, y, 50, 20, 8, 0, "0");
	setitem(hMapd, 0);

	y = y + 20;
	hECTd = CreateCheck(x, y, 130, 20, 8, 0, "Coolant Temp");
	hECTde = CreateEdit(x+130, y, 50, 20, 8, 0, "0");
	setitem(hECTd, 0);

	y = y + -40;
	hIATd = CreateCheck(x+200, y, 130, 20, 8, 0, "Air Temp");
	hIATde = CreateEdit(x+200+130, y, 50, 20, 8, 0, "0");
	setitem(hIATd, 0);

	y = y + 20;
	hRPMd = CreateCheck(x+200, y, 130, 20, 8, 0, "RPM");
	hRPMde = CreateEdit(x+200+130, y, 50, 20, 8, 0, "0");
	setitem(hRPMd, 0);

	y = y + 20;
	hTPSd = CreateCheck(x+200, y, 130, 20, 8, 0, "TPS");
	hTPSde = CreateEdit(x+200+130, y, 50, 20, 8, 0, "0");
	setitem(hTPSd, 0);

	y = y + -40;
	//y = y + 20;
	CreateStatic(x+503, y, 65, 20, 8, 0, "Select Port");
	hPortd = CreateCombo(x+400, y, 100, 100, 8, "ComSel");
	setitem(hPortd, "NONE");
	setitem(hPortd, "COM1");
	setitem(hPortd, "COM2");
	setitem(hPortd, "COM3");
	setitem(hPortd, "COM4");

	y = y + 20;
	CreateStatic(x+503, y, 65, 20, 8, 0, "Select O2");
	hWB = CreateCombo(x+400, y, 100, 100, 8, "WB Sel");
	setitem(hWB, "NONE");
	setitem(hWB, "STOCK");
	setitem(hWB, "INNOVATIVE");
	setitem(hWB, "PLX");
	setitem(hWB, "TE V1/1.5");
	setitem(hWB, "TE V2");
	setitem(hWB, "FJO");
//	setitem(hWB, "AEM");

	y = y + 23;
	hVSSd = CreateCheck(x+400, y, 130, 20, 8, 0, "MPH");
	hVSSde = CreateEdit(x+400+130, y, 50, 20, 8, 0, "0");
	setitem(hVSSd, 0);

	x = 620;
	y = 10;

	CreateBorder(x, y, 300, 85, 8, 0, "DL Triggers");
	CreateStatic(x+5, y-5, 100, 20, 8, 0, "DL Triggers");

	x = x + 20;
	y = y + 20;

	CreateStatic(x, y, 130, 20, 8, 0, "Min TPS");
	hnTPS = CreateEdit(x+130, y, 50, 20, 8, 0, "0");
	setitem(hnTPS, 0);

	y = y + 20;

	CreateStatic(x, y, 130, 20, 8, 0, "Tip-in delay(sec)");
	hnTIP = CreateEdit(x+130, y, 50, 20, 8, 0, "0");
	setitem(hnTIP, 0.5);

	y = y + 20;

	CreateStatic(x, y, 130, 20, 8, 0, "Min RPM");
	hnRPM = CreateEdit(x+130, y, 50, 20, 8, 0, "0");
	setitem(hnRPM, 0);

	assemble("L1H1_AF.asm", 0x8000);
}

num sreply = 0;

void DoGraph()
{
	num log = 1;
	sreply = getitem(hTPSde);
	if(abs(sreply - nLastTps) < 5)
	{
		nTpsTick = nTpsTick + 1;
	}
	else
	{
		log = 0;
		nTpsTick = 0;
	}

	if(nTpsTick < getitem(hnTIP) * 4)
	{
		log = 0;
	}

	if(sreply < getitem(hnTPS))
	{
		log = 0;
	}
	nLastTps = sreply;


	if(getitem(hRPMde) < getitem(hnRPM))
	{
		log = 0;
	}

	if(log)
	{
		num pos = GetItem(hScroll);
		nGraphNo = nGraphNo + 1;
		SetItemEx(hScroll, 0, 0, nGraphNo);
		SetItem(hVSSde, pos);
		SetGraphPos(pos);
		setGraph(0xA, 0);
	}
}

void GraphScroll()
{
	SetGraphPos(GetItem(hScroll));
	refresh(3);
}

void timer250()
{
	if(getitem(ho2d))
	{
		num FJO_AF_Table = 0x8000;
		num TE_AF_Table = 0x81DC;
		num af = 0;
		num x = getitem(hWB);

		serialWrite(0x12);
		sreply = serialRead();

		//None
		if(x == 0)
		{
			af = 0;
		}

		// Stock
		if(x == 1)
		{
			af = sreply;
		}

		// Innovative
		if(x == 2)
		{
			af = (sreply * 0.057624931) + 7.39;
		}

		// PLX
		if(x == 3)
		{
			af = (sreply * 0.0390625) + 10;
		}

		// TE V1.5
		if(x == 4)
		{
			if(sreply <= 70)
				af = 10.00;
			if(sreply >= 149)
				af = 20.00;
			if(af == 0)
			{
				sreply = sreply - 71;
				sreply = sreply * 4;
				af = readfloat(TE_AF_Table+sreply);
			}
		}

		// TE V2
		if(x == 5)
		{
			af = (sreply * 0.0390625) + 9;
		}

		// FJO
		if(x == 6)
		{
			if(sreply <= 40)
			af = 10.00;
			if(sreply >= 160)
				af = 20.00;
			if(af == 0)
			{
				sreply = sreply - 41;
				sreply = sreply * 4;
				af = readfloat(FJO_AF_Table+sreply);
			}
		}

		// AEM
		if(x == 7)
		{
		}

		setitem(ho2de, Round(af, 1));
		setGraph(0x2, af);
	}
	if(getitem(hMapd))
	{
		serialWrite(0x14);
		sreply = serialRead();
		sreply = sreply + 24;
		sreply = sreply * readfloat(MAPSENSOR_FLOAT) * 1000;
		sreply = sreply / 255;
		sreply = Round(sreply, 0);
		setitem(hMapde, sreply);
		setGraph(0x1, sreply);
	}
	if(getitem(hECTd))
	{
		serialWrite(0x10); 
		sreply = serialRead(); 
		sreply = calctemp(sreply);
		setitem(hECTde, Round(sreply, 0)); 
	}
	if(getitem(hIATd))
	{
		serialWrite(0x11);
		sreply = serialRead();
		sreply = calctemp(sreply);
		setitem(hIATde, Round(sreply, 0));
		setGraph(0x5, sreply);
	}
	if(getitem(hTPSd))
	{
		serialWrite(0x15);
		sreply = serialRead();
		sreply = sreply * 100;
		sreply = sreply / 255;
		setitem(hTPSde, Round(sreply, 0));
		setGraph(0x4, sreply);
	}
	if(getitem(hVSSd))
	{
		serialWrite(0x20);
		sreply = serialRead();
		sreply = sreply * 0.6;
		setitem(hVSSde, Round(sreply, 0));
		setGraph(0x3, sreply);
	}
	if(getitem(hRPMd))
	{
		num sreply2;
		serialWrite(0x16);
		sreply = serialRead();
		serialWrite(0x17);
		sreply2 = serialRead();
		sreply = makeword(sreply, sreply2);
		sreply = 1875600 / sreply;
		sreply = Round(sreply, 0);
		setitem(hRPMde, sreply);
		setGraph(0x0, sreply);
	}
	DoGraph();
}